package com.yx.system.controller;

import java.util.List;
import java.util.Map;

import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TbBankcard;
import com.yx.system.service.TbBankcardService;
import com.yx.utils.BankUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 银行卡
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-17 15:40:16
 */
@RestController
@RequestMapping("sys/bankcard")
public class TbBankcardController {
	@Autowired
	private TbBankcardService bankcardService;

	@Autowired
	private TbUserService tbUserService;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:bankcard:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TbBankcard> bankcardList = bankcardService.queryList(query);
		int total = bankcardService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bankcardList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	

	/*
	* 获取用户对应的银行卡信息
	*/
	@RequestMapping("/getBankCard")
	@RequiresPermissions("sys:bankcard:getBankCard")
	public R getBankCard(@LoginUser2 SysUser sysUser){
		if (sysUser.getIdentity()==0){
			return R.error("作者才能查看自身信息");
		}
		//查询对应的用户
		TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
		if (tbUser.getIdentity()==0){
			return R.error("用户的身份不是作者");
		}
		//根据用户信息去查询银行卡
		TbBankcard tbBankcard = bankcardService.findByUser(tbUser.getUserId());
		return R.ok().put("bankcard",tbBankcard);
	}

	/*
	* 新增银行卡
	*/
	@RequestMapping("/addBankCard")
	@RequiresPermissions("sys:bankcard:addBankCard")
	public R addBankCard(@LoginUser2 SysUser sysUser,String bankcard,String identity,String code,String phone,String name){
		Assert.isBlank(bankcard,"银行卡账号不能为空");
		Assert.isBlank(identity,"身份证号码不能为空");
		Assert.isBlank(phone,"预留手机号不能为空");
		Assert.isBlank(name,"姓名不能为空");
		if (sysUser.getIdentity()==0){
			return R.error("作者才能查看自身信息");
		}
		//查询对应的用户
		TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
		if (tbUser.getIdentity()==0){
			return R.error("用户的身份不是作者");
		}
		String rcode = redisTemplate.opsForValue().get(phone);
		if (rcode==null){
			return R.error("验证码已过期");
		}
		if (code.equals(rcode)){
			//先根据用户查询有无银行卡
			TbBankcard tbBankcard = bankcardService.findByUser(tbUser.getUserId());
			if (tbBankcard==null){
				//初始化银行卡对象
				tbBankcard = new TbBankcard();
				String bankName = BankUtil.getNameOfBank(String.valueOf(bankcard));
				if (bankName.equals("")){
					return R.error("请填取正确的银行卡号");
				}
				tbBankcard.setBankcard(bankcard);
				tbBankcard.setBankname(bankName);
				tbBankcard.setUserid(tbUser.getUserId());
				tbBankcard.setIdentity(identity);
				tbBankcard.setName(name);
				tbBankcard.setPhone(phone);
				bankcardService.save(tbBankcard);
			}else {
				String bankName = BankUtil.getNameOfBank(String.valueOf(bankcard));
				if (bankName.equals("")){
					return R.error("请填取正确的银行卡号");
				}
				tbBankcard.setBankcard(bankcard);
				tbBankcard.setBankname(bankName);
				tbBankcard.setIdentity(identity);
				tbBankcard.setName(name);
				tbBankcard.setPhone(phone);
				bankcardService.update(tbBankcard);
			}

			return R.ok();
		}else {
			return R.error("验证码错误");
		}



	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:bankcard:info")
	public R info(@PathVariable("id") Integer id){
		TbBankcard bankcard = bankcardService.queryObject(id);
		
		return R.ok().put("bankcard", bankcard);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:bankcard:save")
	public R save(@RequestBody TbBankcard bankcard){
		bankcardService.save(bankcard);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:bankcard:update")
	public R update(@RequestBody TbBankcard bankcard){
		bankcardService.update(bankcard);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:bankcard:delete")
	public R delete(@RequestBody Integer[] ids){
		bankcardService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
